import Taro ,{ Component } from '@tarojs/taro'
import { View , Image} from '@tarojs/components'
import { AtSwipeAction } from "taro-ui"

import DataManager from '../../utils/DataManager.js'

import './myShopList.scss'
import ShopItem from '../../component/ShopItem/shopItem.js'
import ListEmptyView from '../../component/LiseEmptyView/listEmptyView.js'
import emptyIcon from '../../image/ic_empty.png'

import postImage from '../../image/post.png'

import {base_url} from "../../network/config";
import BasePage from "../basePage";
import {COMMON_CONST} from "../../utils/const";

let mPageNum = 1;

export default class myShopList extends BasePage{

  config = {
    navigationBarTitleText: '商家发布'
  }

  constructor() {
    super(...arguments)
    this.state = {
      list: [],
    }
  }

  componentDidMount () {
    Taro.eventCenter.on('shopInfoPost',() => {
      mPageNum = 1;
      this.loadMyShopList(mPageNum)
    })

    this.loadMyShopList(1)
  }

  onPullDownRefresh(){
    mPageNum = 1;
    this.loadMyShopList(mPageNum);
  }

  onReachBottom(){
    mPageNum ++;
    this.loadMyShopList(mPageNum);
  }


  loadMyShopList(pageNo){
    console.log('loadpage------------' + pageNo)

    let _this = this;
    let url = base_url + 'rest/business/myNewsList'
    Taro.request({
      url:url,
      data: {
        token:DataManager.getToken(),
        platform:'android_c',
        weixinId:DataManager.getWeiXinId(),
        loginChannel:'c',
        appId:'KC876D98B30UA40CJSNHI689FB33SDW',
        digest : 'dda484557c12febe48139d8f44051c13',
        pageNo: pageNo,
        pageSize: COMMON_CONST.LIST_SIZE
      },
      method: 'GET',
      success(res) {
        if (!res.data || !res.data.data || !res.data.data.list || res.data.data.list.length < 1){
          if (mPageNum > 1){
            mPageNum --;
          }
        }

        if (!res || !res.data || !res.data.data){
          return;
        }

        let shops = [];
        if (pageNo == 1){
          shops = res.data.data.list
        } else {
          shops = _this.state.list.concat(res.data.data.list)
        }

        _this.setState({
          list: shops
        })
      } ,
      fail:function(){
        if (mPageNum > 1){
          mPageNum --;
        }
      },
      complete:function(){
        Taro.stopPullDownRefresh();
      }
    })
  }

  handleClick = (item , data) =>{
    if (data.text == '完成'){
      this.updateShopInfoStatus(item.id , 2)
    } else if (data.text == '删除'){
      this.updateShopInfoStatus(item.id , 3)
    }
  }

  handlePostClick = () =>{
    Taro.navigateTo({
      url: '/pages/post/shopInfoPost'
    })
  }

  //jobStatus   2完成  3 删除
  updateShopInfoStatus(shopInfoId , shopInfoStatus){

    console.log('shangchu-===========' + shopInfoId + '   ' + shopInfoStatus)

    let _this = this;
    Taro.request({
      url:base_url + 'stateless/business/updateBusinessNews',
      data:{
        token:DataManager.getToken(),
        platform:'android_c',
        weixinId:DataManager.getWeiXinId(),
        loginChannel:'wechat',
        appId:'KC876D98B30UA40CJSNHI689FB33SDW',
        digest : 'dda484557c12febe48139d8f44051c13',
        id:shopInfoId,
        newsStatus:shopInfoStatus
      },
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      method: 'POST',
      success(res2) {
        Taro.eventCenter.trigger('shopInfoPost' ,[]);
        console.log('success ==========' + JSON.stringify(res2))
        mPageNum = 1;
        _this.loadMyShopList(mPageNum)
      }
    })
  }

  render(){

    let options = [ { text: '完成', style: { backgroundColor: '#6190E8' } }, { text: '删除', style: { backgroundColor: '#FF4949' } }];

    return(
      <View className='my-shop-list-container'>
        {this.state.list.length > 0 ? this.state.list.map(item => {
          return (
            <AtSwipeAction autoClose  onClick={this.handleClick.bind(this , item)} options={options} >
              <ShopItem className='shop-list-item' shopData={item} isMine={true}></ShopItem>
            </AtSwipeAction>
          )
        })
        :
        <ListEmptyView emptyText='暂无数据' emptyImage={emptyIcon}/>
        }
        <View className='post-entry' >
          <Image  src={postImage} style='width:50px; height:50px;position:relative' onClick={this.handlePostClick.bind(this)} />
        </View>
      </View>
    )
  }

}

